#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <dirent.h>
#include <unistd.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <errno.h>
#include "file-list.h"
#include "dir.h"

static int protocol_attri(char * name, char * time,  long size, unsigned int count);
static int analysis_file_time(char * filename, char * time);


static int read_file_list(char * path)
{
  DIR * dir;
  struct dirent * ptr;
  char base[512];
  struct stat st;
  char time[20] = {0};
  unsigned int count = 0;
  int ret = -1;

  if ((dir = opendir(path)) == NULL) {
    perror("open dir error...");
    exit(1);
  }

  while ((ptr = readdir(dir)) != NULL) {
    if (strcmp(ptr->d_name, ".") == 0 || strcmp(ptr->d_name, "..") == 0)
      continue;
    else if (ptr->d_type == 8) { 		/* file */
      //printf("file:%s/%s\n", path, ptr->d_name);
	}
    else if (ptr->d_type == 10)	{	/* link file */
      //printf("link:%s/%s\n", path, ptr->d_name);
	}
    else if (ptr->d_type == 4) {	/* dir */
      memset(base, 0, sizeof base);
      strcpy(base, path);
      strcat(base, "/");
      strcat(base, ptr->d_name);
      read_file_list(base);
    }
	
	chdir(path);
    ret = stat(ptr->d_name, &st);
	if (ret == 0) {
		//printf("size = %ld(byte)\n", st.st_size);

		analysis_file_time(ptr->d_name, time);
		protocol_attri(ptr->d_name, time, st.st_size, count);
		memset(time, 0, sizeof time);
		count++;
	}

	#if 0
    printf("size = %f(kb) %s %s %s\n", (float)st.st_size/1000.0, 
		ctime(&st.st_ctime),  /* created  */
		ctime(&st.st_atime),  /* access   */
		ctime(&st.st_mtime)); /* modified */
	#endif
  }
  
  closedir(dir);
  
  return 1;
}

struct file_list_attribute f_attri[20] = {0};
static unsigned int item = 0;

static int protocol_attri(char * name, char * time,  long size, unsigned int count)
{	
	//printf("protocol_attri:[%s    %s    %d    %d]\n", name, time, strlen(name), strlen(time));

	if (count >= 20) {
		return -1;
	}
	else {
		strcpy(f_attri[count].file_name , name);
		strcpy(f_attri[count].file_time , time);
		f_attri[count].file_size = size;
		//printf("==f_attri[%d]     (%s      ---      %s) \n", count, f_attri[count].file_name, f_attri[count].file_time);
		item = count;
	}

	return 0;
}

static float cal_file_size(long size)
{
	//printf("size = %ld\n", size);

	if (size <= 0) {
		return 0.0;
	}
	else {
		return ((float)size / 1000);
	}
}

static unsigned char CheckSum_4D(char* string, int stringSize){
    unsigned char checkSum = 0xFF;
    long tmp = string[0];
    int i =1;

    for (i = 1; i < stringSize; i++) {
        tmp ^= string[i];
    }

    checkSum = (tmp & 0xff);
    return checkSum;
}

/*
 * $FILELIST,file1,time1,size1,M,fil2,t2,s2,K*4d\r\n
 */
static char * send_package(unsigned int count)
{
	char * package = (char *) malloc (sizeof(char) * 1024*1024);	/* 1M */
	int i = 0;
	char f_size[16] = {0};

	if (count <= 0) {
		return NULL;
	}

	//printf("item = %d\n", item);
	memset(package, 0, sizeof package);
	strcpy(package, "$FILELIST,");

	for (i = 0; i <= count; i++) {
		strcat(package, f_attri[i].file_name);
		strcat(package, (char *)",");
		strcat(package, f_attri[i].file_time);
		strcat(package, (char *)",");
		sprintf(f_size, "%0.3lf", cal_file_size(f_attri[i].file_size));
		//printf("f_size : %s \n", f_size);
		strcat(package, f_size);
		strcat(package, (char *)",");
		memset(f_size, 0, sizeof f_size);
		strcat(package, (char *)"K");
		strcat(package, (char *)",");
	}

	sprintf(f_size, "0x%02x", CheckSum_4D(package+1, strlen(package)));
	//printf("f_size = %s\n", f_size);
	strcat(package, (char *)"*");
	strcat(package, (char *)f_size);
	memset(f_size, 0, sizeof f_size);
	strcat(package, (char *)"\r\n");
	
	// printf("--package--:%s\t len=%d\n", package, strlen(package));
	// free(package);
	item = 0;

	return package;
}

static int analysis_file_time(char * filename, char * time)
{
  //char time[20] = {0};
  unsigned char i;

  //printf("[%s:%s]\n",filename, time);

  strncpy(time, filename, 4);
  time[4] = '-';
  strncpy(time+5, filename+4, 2);
  time[7] = '-';
  strncpy(time+8, filename+6, 2);
  time[10] = ' ';
  strncpy(time+11, filename+9, 2);
  time[13] = ':';
  strncpy(time+14, filename+11, 2);
  time[16] = ':';
  strncpy(time+17, filename+13, 2);

  //printf("time = %s\n", time);

  return 1;
}

static int get_directory_name(char * dir_name)
{
		FILE *fp;	
	char buf[128];
	int count = 0, in = 0;
	char *pdata[32];
	char *pbuf;

	fp = fopen(SN_TXT, "r");
	if (fp == NULL) {
		printf("/tmp/sn.txt open error!\n");
		return -1;
	}

	while (!feof(fp)) {
		memset(buf, 0, sizeof(buf));
		memset(pdata, 0, sizeof(pdata));
		fscanf(fp, "%s", buf);
		
		//printf("====bus:%s\n", buf);
		strcpy(dir_name, buf);
		break;
	}
	fclose(fp);

	return count;
}

char * get_file_list(char * path)
{  
  char * package;
  
  read_file_list(path);
  package = send_package(item);

  return package;
}

void test()
{
	char * package;
	char * path = "/home/share/work/test-verification/dir/test";

	package = get_file_list(path);
	printf("--package--:%s\t len=%ld\n", package, strlen(package));
}

int main(void)
{
	char * package;
	char path[20] = "/mnt/";
	char name[15] = {0};

	test();

#if 0
	memset(name, 0, sizeof name);
	if (get_directory_name(name) == -1) {
		return -1;
	}
	else {
		strcpy(path+5, name);
		printf("path = %s\n", path);
		package = get_file_list(path);
		printf("--package--:%s\t len=%ld\n", package, strlen(package));
	}
#endif

}
